package zhou.jan21;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Register2
 */
public class Register2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String name=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		System.out.println("name="+name+"   pwd="+pwd);
		
		if(pwd!=""){		
		UserService2 us=new UserService2();
		User u=new User(name,pwd);
		boolean result=false;        //默认false，有异常也是false
		  try {
			result=us.register(u);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();       //返回false
		}
		  if(result){
			response.sendRedirect("zhouRegisterSuccess.html");
		}
		else{
			response.sendRedirect("zhouRegisterFail.html");
		 }
		}
		else{
			response.sendRedirect("zhouRegisterFail.html");
	}
	
		
	}
		
		

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
